using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JoyfulHomeJourney.BackEnd.Infrastructure.Atrributes;
using JoyfulHomeJourney.BackEnd.Infrastructure.Dtos.Requests.Objects;
using JoyfulHomeJourney.BackEnd.Infrastructure.Dtos.Responses;
using JoyfulHomeJourney.BackEnd.Infrastructure.Dtos.Responses.Objects;
using JoyfulHomeJourney.BackEnd.Infrastructure.EFCore;
using JoyfulHomeJourney.BackEnd.Server.EFCore;
using JoyfulHomeJourney.BackEnd.Server.Instances.Extends.Requests;
using JoyfulHomeJourney.BackEnd.Server.Instances.Extends.Responses;
using JoyfulHomeJourney.BackEnd.Server.Interfaces;
using Microsoft.EntityFrameworkCore;
namespace JoyfulHomeJourney.BackEnd.Server.Instances
{
/// <summary>
/// 一个类包含了服务类所需的所有仓库表的实例，仅提供给该服务类使用，请将所有服务类所需的仓库表的实例都放在该类中。
/// 仓库表的实例指提供了操作数据库指定表的所有方法的对象
/// </summary>
	internal class CarEventsEF
	{
		public CarEventsEF(DbContext dbContext)
		{
			carEventsServerEF = new CarEventsServerEF(dbContext);
            accountServerEF = new SysAccountsServerEF(dbContext);
            sysDictionaryTypesServerEF = new SysDictionaryTypesServerEF(dbContext);
            dictionarysServerEF = new DictionarysServerEF(dbContext);

        }
        public CarEventsServerEF carEventsServerEF { get; set; }
        public SysAccountsServerEF accountServerEF { get; set; }
        public SysDictionaryTypesServerEF sysDictionaryTypesServerEF { get; set; }
        public DictionarysServerEF dictionarysServerEF { get; set; }

    }
    [InjectClass]
	public class CarEventsServer : ICarEventsServer
	{
        private readonly CarEventsEF _carEventsEF;//包含所有所需仓库表实现的对象
		// 请不要修改该构造方法,也无需添加属性，而应该在上面的EF类中添加属性和构造函数赋值语句
		public CarEventsServer(DbContext dbContext)
		{
            _carEventsEF = new CarEventsEF(dbContext);
            
        }
        public async Task<ResponseApiDto> AddAsync(RequestCarEventsDto dto,string uid)
        {
            CarEvents data=dto.ToCreateEntity();
            data.CePublisher=uid;
            return ResponseApiDto.Create(await _carEventsEF.carEventsServerEF.AddAsync(data));
        }
        public async Task<ResponseApiDto> DeleteAsync(RequestCarEventsDto dto)
        {
            return ResponseApiDto.Create(await _carEventsEF.carEventsServerEF.DeleteAsync(dto.ToDeleteEntity()));
        }
        public async Task<ResponseApiDto> UpdateAsync(RequestCarEventsDto dto)
        {
            return ResponseApiDto.Create(await _carEventsEF.carEventsServerEF.UpdateAllProperNotNullAsync(dto.ToUpdateEntity()));
        }
        public async Task<ResponseApiDto> PaginationAsync(int page, int limit)
        {
            int total = 0;
            List<ResponseCarEventsDto> data = new List<ResponseCarEventsDto>();
            List<CarEvents> carEventss = await _carEventsEF.carEventsServerEF.Pagination1(d => true, false, d => d.CeCreateAt, out total, page, limit).ToListAsync();
            foreach (var carEvents in carEventss)
            {
                data.Add(carEvents.ToResponseDto());
            }
            return ResponseApiDto.Success("数据已获取", new ResponsePaginationDto { total = total, data = data });
        }
        /// <summary>
        /// 查询进行中
        /// </summary>
        /// <param></param>
        /// <returns></returns>
        public async Task<ResponseApiDto> Get_underway(int page, int limit, string key)
        {
            int total = 0;
            key = key ?? "";
            List<ResponseCarEventsDto> data = new List<ResponseCarEventsDto>();
            List<CarEvents> carEventss = await _carEventsEF.carEventsServerEF.Pagination1(d => d.CeTitle.Contains(key)&&(d.CeStatus== "planned"||d.CeStatus== "ongoing"), false, d => d.CeCreateAt, out total, page, limit).ToListAsync();
            foreach (var item in carEventss)
            {
                data.Add(new ResponseCarEventsDto
                {
                    id=item.CeId,
                    createAt=item.CeCreateAt,
                    startTime=item.CeStartTime,
                    description=item.CeDescription,
                    updateAt=item.CeUpdateAt,
                    count=item.CeCount,
                    publisher=item.CePublisher,
                    publisherName=await _carEventsEF.accountServerEF.Query(d=>d.AId==item.CePublisher).Select(d=>d.AName).SingleOrDefaultAsync(),
                    endTime=item.CeEndTime,
                    eventType=item.CeEventType,
                    eventTypeName=await _carEventsEF.dictionarysServerEF.Query(d=>d.DId==item.CeEventType).Select(d=>d.DValue).SingleOrDefaultAsync(),
                    location=item.CeLocation,
                    status=item.CeStatus,
                    title=item.CeTitle,
                });
            }
            return ResponseApiDto.Success("数据已获取", new ResponsePaginationDto { total = total, data = data });
        }
        /// <summary>
        /// 查询结束的活动
        /// </summary>
        /// <param></param>
        /// <returns></returns>
        public async Task<ResponseApiDto> Get_js_underway(int page, int limit, string key)
        {
            int total = 0;
            key = key ?? "";
            List<ResponseCarEventsDto> data = new List<ResponseCarEventsDto>();
            List<CarEvents> carEventss = await _carEventsEF.carEventsServerEF.Pagination1(d => d.CeTitle.Contains(key) && (d.CeStatus == "completed" || d.CeStatus == "cancelled"), false, d => d.CeCreateAt, out total, page, limit).ToListAsync();
            foreach (var item in carEventss)
            {
                data.Add(new ResponseCarEventsDto
                {
                    id = item.CeId,
                    createAt = item.CeCreateAt,
                    startTime = item.CeStartTime,
                    description = item.CeDescription,
                    updateAt = item.CeUpdateAt,
                    publisher = item.CePublisher,
                    publisherName = await _carEventsEF.accountServerEF.Query(d => d.AId == item.CePublisher).Select(d => d.AName).SingleOrDefaultAsync(),
                    endTime = item.CeEndTime,
                    eventType = item.CeEventType,
                    eventTypeName = await _carEventsEF.dictionarysServerEF.Query(d => d.DId == item.CeEventType).Select(d => d.DValue).SingleOrDefaultAsync(),
                    location = item.CeLocation,
                    status = item.CeStatus,
                    title = item.CeTitle,
                });
            }
            return ResponseApiDto.Success("数据已获取", new ResponsePaginationDto { total = total, data = data });
        }
    }
}
